package com.lzw.service.impl;

import com.lzw.domain.dto.SQLFormat;
import com.lzw.domain.vo.ResponseVO;
import com.lzw.mapper.BoxmessageMapper;
import com.lzw.service.Line4Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
public class Line4ServiceImpl implements Line4Service {

    @Autowired
    private BoxmessageMapper mapper;

    /**
     * 生产数量
     */
    private Integer productionNum;

    /**
     * 合格数量
     */
    private Integer qualifiedNum;

    /**
     * 不合格数量
     */
    private Integer unqualifiedNum;

    /**
     * 合格率
     */
    private Double percentOfPass;

    @Override
    public List<ResponseVO> line4GetMsg(String procedureName) {
        //数据库查出来的结果_15dayMsg
        List<SQLFormat> _15dayMsg = mapper.get15dayMsg2(procedureName);
        //返回的对象
        List<ResponseVO> list = new ArrayList<>();
        ResponseVO responseVO;
        //此工序与其他三个工序字段值不一样（合格+不合格）
        if (_15dayMsg.get(0).getName() == "合格数量" || "合格数量".equals(_15dayMsg.get(0).getName())) {
            for (int i = 0; i < _15dayMsg.size(); ) {
                qualifiedNum = _15dayMsg.get(i).getValue();
                unqualifiedNum = _15dayMsg.get(i + 1).getValue();
                productionNum = qualifiedNum + unqualifiedNum;
                if (productionNum == 0) {
                    responseVO = new ResponseVO(_15dayMsg.get(i).getT(), _15dayMsg.get(i).getAlias(), 0, 0, 0, 0.0);
                    list.add(responseVO);
                    i += 2;
                    continue;
                }
                //合格率=合格数量/生产数量，保留两位小数
                BigDecimal bigDecimal1 = BigDecimal.valueOf(qualifiedNum * 100);
                BigDecimal bigDecimal2 = BigDecimal.valueOf(productionNum);
                BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
                percentOfPass = divide.doubleValue();
                responseVO = new ResponseVO(_15dayMsg.get(i).getT(), _15dayMsg.get(i).getAlias(), productionNum, qualifiedNum, unqualifiedNum, percentOfPass);
                list.add(responseVO);
                i += 2;
            }
        } else {
            //其他三个工序（生产+不合格）
            for (int i = 0; i < _15dayMsg.size(); ) {
                productionNum = _15dayMsg.get(i).getValue();
                if (productionNum == 0) {
                    responseVO = new ResponseVO(_15dayMsg.get(i).getT(), _15dayMsg.get(i).getAlias(), 0, 0, 0, 0.0);
                    list.add(responseVO);
                    i += 2;
                    continue;
                }
                unqualifiedNum = _15dayMsg.get(i + 1).getValue();
                qualifiedNum = productionNum - unqualifiedNum;
                //合格率=合格数量/生产数量，保留两位小数
                BigDecimal bigDecimal1 = BigDecimal.valueOf(qualifiedNum * 100);
                BigDecimal bigDecimal2 = BigDecimal.valueOf(productionNum);
                BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
                percentOfPass = divide.doubleValue();
                responseVO = new ResponseVO(_15dayMsg.get(i).getT(), _15dayMsg.get(i).getAlias(), productionNum, qualifiedNum, unqualifiedNum, percentOfPass);
                list.add(responseVO);
                i += 2;
            }
        }
        return list;
    }
}
